package com.zmn.plat.admin.controller.contract.borrow.record;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zmn.common.constant.StatusConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.mcc.common.dto.staff.StaffDRO;
import com.zmn.plat.model.entity.company.BaseCompany;
import com.zmn.plat.model.entity.contract.borrow.record.ContractBorrowRecord;

/**
 * 合同借阅记录表
 *
 * @author linfeng
 * @since 2020-05-09 11:41:45
 */
@RestController
@RequestMapping("/contract/borrow/record/")
public class ContractBorrowRecordModifyController extends ContractBorrowRecordBaseController {

    /**
     * 新增修改
     *
     * @param contractBorrowRecord
     * @author linfeng
     * @since 2020-05-09 11:41:45
     */
    @RequestMapping(value = "addEdit")
    @RequiresPermissions("/contract/borrow/record/addEdit.action")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody ContractBorrowRecord contractBorrowRecord) {
        Date now = DateUtil.getNow();
        String opertor = getMcStaffRealName();
        try {
            ResponseDTO<StaffDRO> data = staffListRemoteService.getStaff(contractBorrowRecord.getBorrower());
            StaffDRO borrowerStaff = null;
            if (data.isSuccess()) {
                borrowerStaff = data.getData();
            }

            if (borrowerStaff != null && borrowerStaff.getCompanyId() != null) {
                contractBorrowRecord.setBorrowerCompanyId(borrowerStaff.getCompanyId());
                BaseCompany baseCompany = baseCompanyService.findByKey(borrowerStaff.getCompanyId());
                if (baseCompany != null) {
                    contractBorrowRecord.setBorrowerCompanyName(baseCompany.getName());
                }
            }

            contractBorrowRecord.setCreater(opertor);
            contractBorrowRecord.setCreateTime(now);
            contractBorrowRecord.setUpdater(opertor);
            contractBorrowRecord.setUpdateTime(now);
            contractBorrowRecordService.insert(contractBorrowRecord);
            return ResponseDTO.success(StatusConsts.STATUS_SUCCESS, "操作成功");
        } catch (Exception ex) {
            logger.error("新增合同借阅记录失败：contractBorrowRecord:{}", contractBorrowRecord, ex);
            return ResponseDTO.fail("操作失败");
        }
    }
}
